module.exports = function ($scope, $resource, $modal, getDate, str2date, toaster) {

	//搜索我的采纳产品库
	$scope.search = function (pageNo){
		var param = {
			'pageNo': pageNo,
			'pageSize': $scope.itemsPerPage,
			'sale_name': $scope.searchform.sale_name,
			'sale_code': $scope.searchform.sale_code,
			'show': $scope.searchform.show
		}
		$resource('/api/as/tc/assemblesale/assembleSaleList', {}, {}).save(param, function(res){
			if(res.errcode === 0){
                $scope.sale_list = res.data.results;
				$scope.totalItems = res.data.totalRecord;
				$scope.currentPage = pageNo;
			} else {
				toaster.error({title: '', body: res.errmsg});
			}
		});
	}
	
	init();
	//初始化查询产品列表
	function init(){
		$scope.currentPage = 1;			//当前页码
		$scope.itemsPerPage = 10;		//每页显示几条;
		$scope.searchform = {
			'showList': [
				{'name': '全部', 'value': ''},
				{'name': '显示', 'value': '1'},
				{'name': '隐藏', 'value': '0'},
			]
		}
		$scope.search(1);
	}

    $scope.addNew = function () {
        var modalInstance = $modal.open({
			template: require('../views/setAssembleSale.html'),
			controller: 'setAssembleSale',
			size: 'lg',
			resolve: {
				getDate: function (utilservice) {
                    return utilservice.getDate;
                },
				'statue': function () {
					return 'new';
				},
				'item': function () {
					return null;
				}
			}
		});
		modalInstance.result.then(function (showResult) {
			$scope.search($scope.currentPage);
		});
	}

	$scope.editSale = function (item) {
		var modalInstance = $modal.open({
			template: require('../views/setAssembleSale.html'),
			controller: 'setAssembleSale',
			size: 'lg',
			resolve: {
				getDate: function (utilservice) {
                    return utilservice.getDate;
                },
				'statue': function () {
					return 'edit';
				},
				'item': function () {
					return item;
				}
			}
		});
		modalInstance.result.then(function (showResult) {
			$scope.search($scope.currentPage);
		});
	}

	// 设置活动产品在微信商城的显示和隐藏
	$scope.setShow = function (item, status) {
		var param = {
			'id': item.id,
			'show': status
		}
		$resource('/api/as/tc/assemblesale/setAssembleShow', {}, {}).save(param, function(res){
			if(res.errcode === 0){
				toaster.success({title: '', body: '成功'});
				item.show = status;
			} else {
				toaster.error({title: '', body: '失败，请重试'});
			}
		});
	}

	// 设置活动产品在微信商城的显示和隐藏
	$scope.delete = function (item, status) {
		if(confirm('确认删除此活动吗？')){
			$resource('/api/as/tc/assemblesale/deleteAssembleSale', {}, {}).save({'id':item.id}, function(res){
				if(res.errcode === 0){
					toaster.success({title: '', body: '成功'});
					$scope.search($scope.currentPage);
				} else {
					toaster.error({title: '', body: '失败，请重试'});
				}
			});
		}
	}


};